//主要交互逻辑

//seckill.detail,init(params);
var seckill = {
    //封装秒杀相关ajax的URL
    URL:{
        now : function () {
            return "/seckill/time/now";
        },
        exportUrl : function (seckillId) {
            return "/seckill/"+seckillId+"/exportUrl";
        },
        excution : function (seckillId,md5) {
            return "/seckill/"+seckillId+"/"+md5+"/excution";
        }
    },
    handleSeckillKill : function(seckillId,node){
        //处理秒杀逻辑
        node.hide()
            .html("<button class='btn btn-primary btn-lg' id='killBtn'>开始秒杀</button>");
        $.post(seckill.URL.exportUrl(seckillId),{},function (result) {
            //交互流程
            if(result && result['success']){
                //可以秒杀就不显示时间图标
                node.prev().hide();
                var exportUrl = result['data'];
                if(exportUrl['exposed']){
                    //开启秒杀
                    var killUrl = seckill.URL.excution(seckillId,exportUrl['md5']);
                    console.log("killUrl"+killUrl);
                    $("#killBtn").one("click",function () {
                        $(this).addClass("disabled");
                        $.post(killUrl,{},function (result) {
                            if(result && result['success']){
                                var killResult = result['data'];
                                var state = killResult["state"];
                                var stateInfo = killResult["stateInfo"];
                                //显示秒杀结果
                                node.html("<span class='label label-success'>"+stateInfo+"</span>");
                            }
                        })
                    });
                    node.show();
                }else{
                    //未开启秒杀
                    var now  = exportUrl['now'];
                    var start = exportUrl['start'];
                    var end = exportUrl['end'];
                    //重新计算计时逻辑
                    seckill.countDownTime(seckillId,now,start,end);
                }
            }else{
                console.log("result:"+result);
            }
        });

    },
    countDownTime : function(seckillId,nowTime,startTime,endTime){
        //时间判断
        var seckillBox = $("#seckill-box");
        if(nowTime > endTime){
            seckillBox.html("秒杀结束！");
        }else if( nowTime < startTime){
            var killTime = new Date(startTime+1000);
            seckillBox.countdown(killTime,function (event) {
                var format = event.strftime("秒杀倒计时: %D天 %H时 %M分  %S秒");
                seckillBox.html(format);
            }).on("finish.countdown",function () {
                //获取秒杀地址
                seckill.handleSeckillKill(seckillId,seckillBox);
            });
        }else{
            //时间已经在秒杀时间内
            seckill.handleSeckillKill(seckillId,seckillBox);
        }

    },
    validatePhone : function(phone){
        if(phone && phone.length == 11 && ! isNaN(phone)){
            return true;
        }else {
            return false;
        }

    },
    //详情页秒杀逻辑
    detail:{
        //详情页初始化
        init :function (params) {
            //手机验证登录 计时交互
            var killPhone = $.cookie("killPhone");
                    console.info("killPhone=="+killPhone);
            if(!seckill.validatePhone(killPhone)){
                var killPhoneModal =$("#killPhoneModal");
                killPhoneModal.modal({
                    show:true,
                    backdrop:"static", //禁止位置关闭
                    keyboard:false
                });

                $("#killPhoneBtn").click(function () {
                    var inputPhone = $("#killPhoneKey").val();
                    if(seckill.validatePhone(inputPhone)){
                        //刷新页面
                        $.cookie("killPhone",inputPhone,{expires:7,path:"/seckill"});
                        window.location.reload();
                    }else{
                        $("#killPhoneMessage").hide().html("<label class='label label-danger'>手机号错误！</label>").show(300);
                    }
                });
            }
            //计时交互
            var startTime = params["startTime"];
            var endTime = params["endTime"];
            var seckillId = params["seckillId"];
            $.get(seckill.URL.now(),{},function (result) {
                if(result && result["success"]){
                    var nowTime = result["data"];
                    seckill.countDownTime(seckillId,nowTime,startTime,endTime);
                }else{
                    console.log("result:"+result);
                }
            });

        }

    }



}